
package service

import (
	"server/global"
	"server/plugin/mall/model"
    "server/plugin/mall/model/request"
    "gorm.io/gorm"
)

var A订单 = new(dingdan)

type dingdan struct {}
// CreateA订单 创建订单记录
// Author [yourname](https://github.com/yourname)
func (s *dingdan) CreateA订单(dingdan *model.A订单) (err error) {
	err = global.GVA_DB.Create(dingdan).Error
	return err
}

// DeleteA订单 删除订单记录
// Author [yourname](https://github.com/yourname)
func (s *dingdan) DeleteA订单(ID string,userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&model.A订单{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
              return err
        }
        if err = tx.Delete(&model.A订单{},"id = ?",ID).Error; err != nil {
              return err
        }
        return nil
	})
	return err
}

// DeleteA订单ByIds 批量删除订单记录
// Author [yourname](https://github.com/yourname)
func (s *dingdan) DeleteA订单ByIds(IDs []string,deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&model.A订单{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
            return err
        }
        if err := tx.Where("id in ?", IDs).Delete(&model.A订单{}).Error; err != nil {
            return err
        }
        return nil
    })
	return err
}

// UpdateA订单 更新订单记录
// Author [yourname](https://github.com/yourname)
func (s *dingdan) UpdateA订单(dingdan model.A订单) (err error) {
	err = global.GVA_DB.Model(&model.A订单{}).Where("id = ?",dingdan.ID).Updates(&dingdan).Error
	return err
}

// GetA订单 根据ID获取订单记录
// Author [yourname](https://github.com/yourname)
func (s *dingdan) GetA订单(ID string) (dingdan model.A订单, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&dingdan).Error
	return
}

// GetA订单InfoList 分页获取订单记录
// Author [yourname](https://github.com/yourname)
func (s *dingdan) GetA订单InfoList(info request.A订单Search) (list []model.A订单, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.A订单{})
    var dingdans []model.A订单
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }
	err = db.Find(&dingdans).Error
	return  dingdans, total, err
}
func (s *dingdan)GetA订单DataSource() (res map[string][]map[string]any, err error) {
	res = make(map[string][]map[string]any)
	
	   用户id := make([]map[string]any, 0)
	   
       
       global.GVA_DB.Table("sys_users").Where("deleted_at IS NULL").Select("username as label,id as value").Scan(&用户id)
	   res["用户id"] = 用户id
	return
}

func (s *dingdan)GetA订单Public() {

}